//
//  delimitedfilereader.cpp
//  IO
//
//  Created by Mark Lord on 2013-06-16.
//  Copyright (c) 2013 Mark Lord. All rights reserved.
//

#include "delimitedfilereader.h"
#include <boost/tokenizer.hpp>
#include <boost/algorithm/string.hpp>

//********************************************************
// Constructor
//********************************************************
DelimitedFileReader::DelimitedFileReader(std::string _filePath, std::string _delim)
: Datareader(), m_delim(_delim)
{
    // Open the file
    m_inFile.open(_filePath);
    
    // Initialize our data vector
    this->m_pData.reset(new std::vector<Record>());
}

//********************************************************
// Destructor
//********************************************************
DelimitedFileReader::~DelimitedFileReader()
{
    this->m_inFile.close();
}

//********************************************************
// This method reads one line of the file
//********************************************************
bool DelimitedFileReader::read()
{
    if (!this->m_inFile.eof())
    {
        this->m_pData->push_back(Record());
        std::string line;
        std::getline(this->m_inFile, line);
        boost::split(this->m_pData->back(), line, boost::is_any_of(m_delim), boost::token_compress_on);
        
        return true;
    }
    
    return false;
}

//********************************************************
// This method reads all lines of the file
//********************************************************
void DelimitedFileReader::readAll()
{
    while (this->read());
}

//********************************************************
// This gets the last read record
/*
 /return
  /li The last read record.
 */
//********************************************************
Record & DelimitedFileReader::get()
{
    return this->m_pData->back();
}

//********************************************************
// This gets all records currently read in
/*
 /return
  /li A vector of all currently read in records
 */
//********************************************************
boost::shared_ptr<std::vector<Record>> DelimitedFileReader::getAll()
{
    return this->m_pData;
}